import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
          useMaterial3: false,
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Flutter App"),
          ),
          body: const MyHome(),
        ));
  }
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {

  //创建Form组件全局key
  final _globalKey = GlobalKey<FormState>();

  String? _username = "";
  String? _password = "";
  String? _phone = "";
  String? _birthday = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      //设置Form表单key
      key: _globalKey,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          //表单组件
          TextFormField(
            keyboardType: TextInputType.text,
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.account_box),
              hintText: "请输入姓名",
              // labelText: "请输入姓名",
            ),
            //表单组件验证方法
            validator: (value){
              if(value!.isEmpty || value == null){
                return "姓名不能为空";
              }
              return null;
            },
            onSaved: (value){
              _username = value;
            },
          ),
          TextFormField(
            obscureText: true,
            keyboardType: TextInputType.text,
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.password),
              hintText: "请输入密码",
              // labelText: "请输入姓名",
            ),
            validator: (value){
              if(value!.isEmpty || value == null){
                return "密码不能为空";
              }
              return null;
            },
            onSaved: (value){
              _password = value;
            },
          ),
          TextFormField(
            keyboardType: TextInputType.phone,
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.phone),
              hintText: "请输入手机号",
              // labelText: "请输入姓名",
            ),
            validator: (value){
              if(value!.isEmpty || value == null){
                return "手机号不能为空";
              }
              return null;
            },
            onSaved: (value){
              _phone = value;
            },
          ),
          TextFormField(
            textInputAction: TextInputAction.done,
            keyboardType: TextInputType.datetime,
            decoration: const InputDecoration(
              prefixIcon: Icon(Icons.calendar_view_day),
              hintText: "请输入生日",
              // labelText: "请输入姓名",
            ),
            validator: (value){
              if(value!.isEmpty || value == null){
                return "生日不能为空";
              }
              return null;
            },
            onSaved: (value){
              _birthday = value;
            },
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: 1,
                child: Padding(
                  padding: const EdgeInsets.all(10),
                  child: ElevatedButton(
                      style: const ButtonStyle(
                          padding: MaterialStatePropertyAll(
                              EdgeInsets.all(10)
                          )
                      ) ,
                      onPressed: (){
                        //通过globalKey的state的validate(),触发验证操作
                        //如果验证失败会显示validate()中的return的提示信息
                        bool? validateFlag =  _globalKey.currentState?.validate();
                        print(validateFlag);
                        if(validateFlag!){
                          ScaffoldMessenger.of(context)
                              .showSnackBar(SnackBar(content: Text('输入内容有效')));
                          //save方法会触发Form表单中每个组件的save()方法
                          //因此我们可以在组件的save方法中，进行变量赋值操作
                          _globalKey.currentState!.save();
                          print(_username);
                          print(_password);
                          print(_phone);
                          print(_birthday);
                        }
                      },
                      child: const Text("注册",style: TextStyle(fontSize: 20))
                  ),
                )
              ),
            ],
          )
        ],
      )
    );
  }
}
